//绘制尸体
function drawCorpse(ctx) {
  for (
    var i = 0, corpseListLength = corpseList.length;
    i < corpseListLength;
    i++
  ) {
    var corpse = corpseList[i];
    var velocityX = Math.cos(corpse.radians) * corpse.distance;
    var velocityY = Math.sin(corpse.radians) * corpse.distance;

    corpse.x = corpse.x - velocityX;
    corpse.y = corpse.y - velocityY;
    ctx.beginPath();
    ctx.ellipse(
      corpse.x,
      corpse.y,
      (corpse.r * corpse.delay) / 30,
      (corpse.r * corpse.delay) / 45,
      corpse.delay / 8,
      0,
      Math.PI * 2
    );
    ctx.fillStyle = "#000";
    ctx.fill();
    corpse.delay--;
  }
  corpseList = corpseList.filter((item) => {
    return item.delay > 0;
  });
}

//绘制枪
function drawGun(ctx) {
  for (var i = 0, gunListLength = gunList.length; i < gunListLength; i++) {
    var gun = gunList[i];
    var isAction = false;
    for (
      var j = 0, bulletListLength = bulletList.length;
      j < bulletListLength;
      j++
    ) {
      var bullet = bulletList[j];
      //排除反弹以及贯穿的子弹
      if (bullet.bounce !== bounce || bullet.pierce !== pierce) {
        continue;
      }
      if (gun.position === bullet.position) {
        if (bullet.currentPath > 1 && bullet.currentPath < 5) {
          ctx.beginPath();
          ctx.arc(
            bullet.baseX +
              dpr * Math.cos(bullet.radians) * bullet.currentPath * 2,
            bullet.baseY +
              dpr * Math.sin(bullet.radians) * bullet.currentPath * 2,
            r / 2.5,
            0,
            2 * Math.PI
          );
          ctx.fillStyle = "#000000";
          ctx.fill();
          ctx.closePath();
          isAction = true;
        }
        if (bullet.currentPath >= 5 && bullet.currentPath < 10) {
          ctx.beginPath();
          ctx.arc(
            bullet.baseX +
              dpr * Math.cos(bullet.radians) * (10 - bullet.currentPath) * 2,
            bullet.baseY +
              dpr * Math.sin(bullet.radians) * (10 - bullet.currentPath) * 2,
            r / 2.5,
            0,
            2 * Math.PI
          );
          ctx.fillStyle = "#000000";
          ctx.fill();
          ctx.closePath();
          isAction = true;
        }
      }
    }

    if (!isAction) {
      ctx.beginPath();
      ctx.arc(gun.baseX, gun.baseY, r / 2.5, 0, 2 * Math.PI);
      ctx.fillStyle = "#000000";
      ctx.fill();
      ctx.closePath();
    }
  }
}

//绘制子弹
function drawBullet(ctx) {
  for (
    var i = 0, bulletListLength = bulletList.length;
    i < bulletListLength;
    i++
  ) {
    var bullet = bulletList[i];
    ctx.save();
    if (bullet.currentPath > 0) {
      //子弹移动，但是逻辑放到这里不合适
      bullet.x = bullet.x - Math.cos(bullet.radians) * bullet.distance;
      bullet.y = bullet.y - Math.sin(bullet.radians) * bullet.distance;

      //椭圆
      ctx.shadowBlur = 4;
      ctx.shadowOffsetX = 0;
      ctx.shadowOffsetY = 0;
      ctx.shadowColor = "#ffdd00";
      ctx.beginPath();
      ctx.ellipse(
        bullet.x,
        bullet.y,
        r / 1.4,
        r / 3.9,
        bullet.radians,
        0,
        Math.PI * 2
      );
      ctx.fillStyle = "#ff5000";
      ctx.fill();
    }
    bullet.currentPath++;

    //射击效果
    //穿透的子弹没有射击效果
    if (bullet.pierce === pierce) {
      if (bullet.currentPath > 1 && bullet.currentPath < 5) {
        ctx.shadowBlur = 4;
        ctx.shadowOffsetX = 0;
        ctx.shadowOffsetY = 0;
        ctx.shadowColor = "#ffdd00";
        ctx.beginPath();
        ctx.arc(
          bullet.fireX,
          bullet.fireY,
          r / 3 + (bullet.currentPath * r) / 30,
          0,
          2 * Math.PI
        );
        ctx.fillStyle = "#ff5000";
        ctx.fill();
      }
      if (bullet.currentPath >= 5 && bullet.currentPath < 10) {
        ctx.shadowBlur = 4;
        ctx.shadowOffsetX = 0;
        ctx.shadowOffsetY = 0;
        ctx.shadowColor = "#ffdd00";
        ctx.beginPath();
        ctx.arc(
          bullet.fireX,
          bullet.fireY,
          r / 1.5 - ((bullet.currentPath - 5) * r) / 30,
          0,
          2 * Math.PI
        );
        ctx.fillStyle = "#ff5000";
        ctx.fill();
      }
    }

    ctx.restore();
  }
}

//绘制守卫
function drawGuard(ctx) {
  if (guardList.length > 0) {
    ctx.globalAlpha = 0.1;
    ctx.beginPath();
    ctx.arc(myX, myY, guardList[0].R, 0, 2 * Math.PI);
    ctx.strokeStyle = "#000000";
    ctx.lineWidth = 1;
    ctx.stroke();
    ctx.closePath();
    ctx.globalAlpha = 1;
  }
  for (
    var i = 0, guardListLength = guardList.length;
    i < guardListLength;
    i++
  ) {
    var guard = guardList[i];
    ctx.beginPath();
    ctx.fillStyle = guard.color;
    ctx.arc(guard.x, guard.y, guard.r, 0, 2 * Math.PI);
    ctx.fill();
    ctx.closePath();
  }
}

//绘制棍子
function drawRok(ctx) {
  for (var i = 0, rokListLength = rokList.length; i < rokListLength; i++) {
    var rok = rokList[i];
    if (rok.duration === 0) {
      var velocityX = Math.cos(rok.radians) * rok.l;
      var velocityY = Math.sin(rok.radians) * rok.l;
      var toX = rok.x + velocityX;
      var toY = rok.y + velocityY;

      ctx.save();
      ctx.strokeStyle = "#08f000";
      ctx.lineWidth = dpr * 7;
      ctx.lineCap = "round";
      ctx.filter = "blur(" + r / 4 + "px)";
      ctx.beginPath();
      ctx.moveTo(rok.x, rok.y);
      ctx.lineTo(toX, toY);
      ctx.stroke();
      ctx.restore();

      ctx.save();
      ctx.strokeStyle = "#f3fef2";
      ctx.lineWidth = dpr * 3;
      ctx.lineCap = "round";
      ctx.beginPath();
      ctx.moveTo(rok.x, rok.y);
      ctx.lineTo(toX, toY);
      ctx.stroke();
      ctx.restore();

      ctx.beginPath();
      ctx.arc(rok.x, rok.y, r / 3, 0, 2 * Math.PI);
      ctx.fillStyle = "#000000";
      ctx.fill();
      ctx.closePath();
    } else {
      if (rok.direction === "left") {
        //绘制攻击时的棍子（三个阶段：拿出、攻击、收回）
        if (rok.duration > 35) {
          //拿出棍子
          rok.x += (rok.headX - rok.x) / (rok.duration - 35);
          rok.y += (rok.headY - rok.y) / (rok.duration - 35);
          rok.radians += -(rok.radians - rok.tempRadians) / (rok.duration - 35);
          var velocityX = Math.cos(rok.radians) * rok.l;
          var velocityY = Math.sin(rok.radians) * rok.l;
          var toX = rok.x + velocityX;
          var toY = rok.y + velocityY;
          ctx.save();
          ctx.strokeStyle = "#08f000";
          ctx.lineWidth = dpr * 7;
          ctx.lineCap = "round";
          ctx.filter = "blur(" + r / 4 + "px)";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.save();
          ctx.strokeStyle = "#f3fef2";
          ctx.lineWidth = dpr * 3;
          ctx.lineCap = "round";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.beginPath();
          ctx.arc(rok.x, rok.y, r / 2.5, 0, 2 * Math.PI);
          ctx.fillStyle = "#000000";
          ctx.fill();
          ctx.closePath();
        } else if (rok.duration <= 35 && rok.duration > 32) {
          var velocityX = Math.cos(rok.radians) * rok.l;
          var velocityY = Math.sin(rok.radians) * rok.l;
          var toX = rok.x + velocityX;
          var toY = rok.y + velocityY;
          ctx.save();
          ctx.strokeStyle = "#08f000";
          ctx.lineWidth = dpr * 7;
          ctx.lineCap = "round";
          ctx.filter = "blur(" + r / 4 + "px)";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.save();
          ctx.strokeStyle = "#f3fef2";
          ctx.lineWidth = dpr * 3;
          ctx.lineCap = "round";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.beginPath();
          ctx.arc(rok.x, rok.y, r / 2.5, 0, 2 * Math.PI);
          ctx.fillStyle = "#000000";
          ctx.fill();
          ctx.closePath();
        } else if (rok.duration <= 32 && rok.duration > 2) {
          //挥动棍子
          var headRadians = Math.atan2(
            rok.y - rok.centerY,
            rok.x - rok.centerX
          );
          headRadians -= 0.1;
          var headDistance = Math.sqrt(
            Math.pow(rok.x - rok.centerX, 2) + Math.pow(rok.y - rok.centerY, 2)
          );
          rok.x = rok.centerX + Math.cos(headRadians) * headDistance;
          rok.y = rok.centerY + Math.sin(headRadians) * headDistance;
          var velocityX =
            Math.cos(headRadians + Math.PI / 3 - (32 - rok.duration) * 0.06) *
            rok.l;
          var velocityY =
            Math.sin(headRadians + Math.PI / 3 - (32 - rok.duration) * 0.06) *
            rok.l;
          rok.radians = headRadians + Math.PI / 3 - (32 - rok.duration) * 0.07;
          var toX = rok.x + velocityX;
          var toY = rok.y + velocityY;
          ctx.save();
          ctx.strokeStyle = "#08f000";
          ctx.lineWidth = dpr * 7;
          ctx.lineCap = "round";
          ctx.filter = "blur(" + r / 4 + "px)";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.save();
          ctx.strokeStyle = "#f3fef2";
          ctx.lineWidth = dpr * 3;
          ctx.lineCap = "round";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.beginPath();
          ctx.arc(rok.x, rok.y, r / 2.5, 0, 2 * Math.PI);
          ctx.fillStyle = "#000000";
          ctx.fill();
          ctx.closePath();
        } else {
          //收回棍子
          rok.x -= (rok.x - (myX + getDeviationX(rok.position))) / rok.duration;
          rok.y -= (rok.y - (myY + getDeviationY(rok.position))) / rok.duration;
          // rok.radians -= (rok.radians - rok.tempRadians) / (rok.duration)
          var velocityX = Math.cos(rok.radians) * rok.l;
          var velocityY = Math.sin(rok.radians) * rok.l;
          var toX = rok.x + velocityX;
          var toY = rok.y + velocityY;
          ctx.save();
          ctx.strokeStyle = "#08f000";
          ctx.lineWidth = dpr * 7;
          ctx.lineCap = "round";
          ctx.filter = "blur(" + r / 4 + "px)";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.save();
          ctx.strokeStyle = "#f3fef2";
          ctx.lineWidth = dpr * 3;
          ctx.lineCap = "round";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.beginPath();
          ctx.arc(rok.x, rok.y, r / 2.5, 0, 2 * Math.PI);
          ctx.fillStyle = "#000000";
          ctx.fill();
          ctx.closePath();
        }
      } else {
        //绘制攻击时的棍子（三个阶段：拿出、攻击、收回）
        if (rok.duration > 35) {
          //拿出棍子
          rok.x += (rok.headX - rok.x) / (rok.duration - 35);
          rok.y += (rok.headY - rok.y) / (rok.duration - 35);
          rok.radians -= (rok.radians - rok.tempRadians) / (rok.duration - 35);
          var velocityX = Math.cos(rok.radians) * rok.l;
          var velocityY = Math.sin(rok.radians) * rok.l;
          var toX = rok.x + velocityX;
          var toY = rok.y + velocityY;
          ctx.save();
          ctx.strokeStyle = "#08f000";
          ctx.lineWidth = dpr * 7;
          ctx.lineCap = "round";
          ctx.filter = "blur(" + r / 4 + "px)";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.save();
          ctx.strokeStyle = "#f3fef2";
          ctx.lineWidth = dpr * 3;
          ctx.lineCap = "round";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();
          ctx.beginPath();
          ctx.arc(rok.x, rok.y, r / 2.5, 0, 2 * Math.PI);
          ctx.fillStyle = "#000000";
          ctx.fill();
          ctx.closePath();
        } else if (rok.duration <= 35 && rok.duration > 32) {
          var velocityX = Math.cos(rok.radians) * rok.l;
          var velocityY = Math.sin(rok.radians) * rok.l;
          var toX = rok.x + velocityX;
          var toY = rok.y + velocityY;
          ctx.save();
          ctx.strokeStyle = "#08f000";
          ctx.lineWidth = dpr * 7;
          ctx.lineCap = "round";
          ctx.filter = "blur(" + r / 4 + "px)";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.save();
          ctx.strokeStyle = "#f3fef2";
          ctx.lineWidth = dpr * 3;
          ctx.lineCap = "round";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.beginPath();
          ctx.arc(rok.x, rok.y, r / 2.5, 0, 2 * Math.PI);
          ctx.fillStyle = "#000000";
          ctx.fill();
          ctx.closePath();
        } else if (rok.duration <= 32 && rok.duration > 2) {
          //挥动棍子
          var headRadians = Math.atan2(
            rok.y - rok.centerY,
            rok.x - rok.centerX
          );
          headRadians += 0.1;
          var headDistance = Math.sqrt(
            Math.pow(rok.x - rok.centerX, 2) + Math.pow(rok.y - rok.centerY, 2)
          );
          rok.x = rok.centerX + Math.cos(headRadians) * headDistance;
          rok.y = rok.centerY + Math.sin(headRadians) * headDistance;
          var velocityX =
            Math.cos(headRadians - Math.PI / 3 + (32 - rok.duration) * 0.06) *
            rok.l;
          var velocityY =
            Math.sin(headRadians - Math.PI / 3 + (32 - rok.duration) * 0.06) *
            rok.l;
          rok.radians = headRadians - Math.PI / 3 + (32 - rok.duration) * 0.07;
          var toX = rok.x + velocityX;
          var toY = rok.y + velocityY;
          ctx.save();
          ctx.strokeStyle = "#08f000";
          ctx.lineWidth = dpr * 7;
          ctx.lineCap = "round";
          ctx.filter = "blur(" + r / 4 + "px)";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.save();
          ctx.strokeStyle = "#f3fef2";
          ctx.lineWidth = dpr * 3;
          ctx.lineCap = "round";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.beginPath();
          ctx.arc(rok.x, rok.y, r / 2.5, 0, 2 * Math.PI);
          ctx.fillStyle = "#000000";
          ctx.fill();
          ctx.closePath();
        } else {
          //收回棍子
          rok.x -= (rok.x - (myX + getDeviationX(rok.position))) / rok.duration;
          rok.y -= (rok.y - (myY + getDeviationY(rok.position))) / rok.duration;
          // rok.radians += (rok.radians - rok.tempRadians) / (rok.duration)
          var velocityX = Math.cos(rok.radians) * rok.l;
          var velocityY = Math.sin(rok.radians) * rok.l;
          var toX = rok.x + velocityX;
          var toY = rok.y + velocityY;
          ctx.save();
          ctx.strokeStyle = "#08f000";
          ctx.lineWidth = dpr * 7;
          ctx.lineCap = "round";
          ctx.filter = "blur(" + r / 4 + "px)";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.save();
          ctx.strokeStyle = "#f3fef2";
          ctx.lineWidth = dpr * 3;
          ctx.lineCap = "round";
          ctx.beginPath();
          ctx.moveTo(rok.x, rok.y);
          ctx.lineTo(toX, toY);
          ctx.stroke();
          ctx.restore();

          ctx.beginPath();
          ctx.arc(rok.x, rok.y, r / 2.5, 0, 2 * Math.PI);
          ctx.fillStyle = "#000000";
          ctx.fill();
          ctx.closePath();
        }
      }

      if (rok.duration > 0) {
        rok.duration--;
        if (rok.duration === 0) {
          rok.x = myX + getDeviationX(rok.position);
          rok.y = myY + getDeviationY(rok.position);
        }
      }
    }
  }
}

//绘制火花
function drawSpark(ctx) {
  for (
    var i = 0, sparkListLength = sparkList.length;
    i < sparkListLength;
    i++
  ) {
    var spark = sparkList[i];
    if (spark.duration > 0) {
      ctx.beginPath();
      ctx.arc(spark.x, spark.y, spark.r, 0, 2 * Math.PI);
      spark.r = spark.r * 0.95;
      ctx.fillStyle = spark.color;
      ctx.fill();

      var velocityX = Math.cos(spark.radians) * spark.range * 0.1;
      var velocityY = Math.sin(spark.radians) * spark.range * 0.1;
      spark.x = spark.x + velocityX;
      spark.y = spark.y + velocityY;

      spark.duration--;
    }
  }

  sparkList = sparkList.filter((item) => {
    return item.duration > 0;
  });
}

//绘制甩尾
function drawDrift(ctx) {
  for (
    var i = 0, driftListLength = driftList.length;
    i < driftListLength;
    i++
  ) {
    var drift = driftList[i];
    if (drift.duration > 0) {
      ctx.beginPath();
      ctx.arc(drift.x, drift.y, drift.r, 0, 2 * Math.PI);
      drift.r = drift.r * drift.decay;
      ctx.fillStyle = drift.color;
      ctx.fill();
      drift.duration--;
    }
  }

  driftList = driftList.filter((item) => {
    return item.duration > 0;
  });
}

//绘制爆炸火花
function drawBoomSpark(ctx) {
  for (
    var i = 0, boomSparkListLength = boomSparkList.length;
    i < boomSparkListLength;
    i++
  ) {
    var boomSpark = boomSparkList[i];
    if (boomSpark.duration > 0) {
      // ctx.beginPath();
      // ctx.arc(boomSpark.x, boomSpark.y, boomSpark.r, 0, 2 * Math.PI);
      // boomSpark.r = boomSpark.r * 0.95
      // ctx.fillStyle = boomSpark.color;
      // ctx.fill();

      ctx.save();
      // ctx.shadowBlur = r / 4;
      // ctx.shadowOffsetX = 0;
      // ctx.shadowOffsetY = 0;
      // ctx.shadowColor = "#444444";
      ctx.beginPath();
      ctx.arc(boomSpark.x, boomSpark.y, boomSpark.r, 0, 2 * Math.PI);
      ctx.fillStyle = "#444444";
      ctx.fill();
      ctx.closePath();
      ctx.shadowBlur = 0;
      ctx.beginPath();
      ctx.arc(boomSpark.x, boomSpark.y, boomSpark.r * 0.9, 0, 2 * Math.PI);
      ctx.fillStyle = "#ff0000";
      ctx.fill();
      ctx.closePath();
      ctx.restore();
      boomSpark.r = boomSpark.r * 0.95;

      boomSpark.duration--;
    }
  }

  boomSparkList = boomSparkList.filter((item) => {
    return item.duration > 0;
  });
}

//绘制爆炸
function drawExplosion(ctx) {
  for (
    var i = 0, explosionListLength = explosionList.length;
    i < explosionListLength;
    i++
  ) {
    var explosion = explosionList[i];
    if (explosion.duration > 0) {
      explosion.update();
      explosion.draw(ctx);
    }
  }

  explosionList = explosionList.filter((item) => {
    return item.duration > 0;
  });
}

function calculateEasedY(deduction, dpr) {
  const { duration, y, x } = deduction;
  const minDuration = 38;
  const maxDuration = 50;

  const clampedDuration = Math.max(duration, minDuration);

  const t = (clampedDuration - minDuration) / (maxDuration - minDuration);

  const easedT = t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;

  const minY = y - dpr * 4;
  const maxY = y + dpr * 1;

  return minY + (maxY - minY) * -easedT;
}

//绘制扣血
function drawDeduction(ctx) {
  for (
    var i = 0, deductionListLength = deductionList.length;
    i < deductionListLength;
    i++
  ) {
    var deduction = deductionList[i];
    ctx.font = "bold " + deduction.size + "px SimHei";
    ctx.beginPath();
    if (deduction.color === "#ffffff") {
      ctx.lineWidth = 2;
    } else {
      ctx.lineWidth = 1;
    }
    var tempX = deduction.x;
    var tempY = deduction.y;
    tempY = calculateEasedY(deduction, dpr);
    ctx.strokeText(deduction.n, tempX, tempY);
    ctx.strokeStyle = "#444444";
    ctx.closePath();
    ctx.fillStyle = deduction.color;
    ctx.fillText(deduction.n, tempX, tempY);
    deduction.duration--;
  }

  deductionList = deductionList.filter((item) => {
    return item.duration > 0;
  });
}

//绘制植物
function drawPlant(ctx) {
  for (
    var i = 0, plantListLength = plantList.length;
    i < plantListLength;
    i++
  ) {
    var plant = plantList[i];
    ctx.globalAlpha = 0.15;
    ctx.beginPath();
    ctx.ellipse(
      plant.x,
      plant.y + dpr * 13,
      dpr * 16,
      dpr * 5,
      0,
      0,
      Math.PI * 2
    );
    ctx.fillStyle = "#000";
    ctx.fill();
    ctx.globalAlpha = 1;
    ctx.drawImage(
      plantImage,
      plant.x - dpr * 20,
      plant.y - dpr * 20,
      dpr * 40,
      dpr * 40
    );
  }
}

//绘制Buff
function drawBuff(ctx) {
  for (var i = 0, buffListLength = buffList.length; i < buffListLength; i++) {
    var buff = buffList[i];
    // 设置阴影
    ctx.shadowColor = buff.color; // 阴影颜色和透明度
    ctx.shadowBlur = 6 * dpr + trigonometricValue * dpr * 2.2; // 模糊程度
    ctx.beginPath();
    ctx.arc(buff.x, buff.y, dpr * buff.r * 1.8, 0, 2 * Math.PI);
    ctx.fillStyle = buff.color;
    ctx.fill();
    ctx.closePath();
    ctx.shadowBlur = 0;
    var image = null;
    switch (buff.type) {
      case "speed":
        image = speedImage;
        break;
      case "attack_speed":
        image = attackSpeedImage;
        break;
      case "pickup_range":
        image = pickupRangeImage;
        break;
    }
    ctx.drawImage(
      image,
      buff.x - dpr * 7,
      buff.y - dpr * 7,
      dpr * 14,
      dpr * 14
    );

    buff.duration--;
  }

  buffList = buffList.filter((item) => item.duration > 0);
}

//绘制食物
function drawFood(ctx) {
  for (var i = 0, foodListLength = foodList.length; i < foodListLength; i++) {
    var food = foodList[i];
    food.draw(ctx);
  }
}

//绘制敌方
function drawEnemy(ctx) {
  for (
    var i = 0, enemyListLength = enemyList.length;
    i < enemyListLength;
    i++
  ) {
    var enemy = enemyList[i];

    var subX = 0.8 * dpr * Math.cos(enemy.radians) * 1.2;
    var subY = 0.8 * dpr * Math.sin(enemy.radians) * 1.2;
    if (enemy.moveType === "stay") {
      subX = 0;
      subY = 0;
    } else if (enemy.moveType === "leave") {
      subX = -0.8 * dpr * Math.cos(enemy.radians) * 1.2;
      subY = -0.8 * dpr * Math.sin(enemy.radians) * 1.2;
    }
    ctx.globalAlpha = 0.15;
    ctx.beginPath();
    ctx.ellipse(
      enemy.x - subX,
      enemy.y + enemy.r * 1.1 - subY,
      enemy.r / 1.2 + subY * 1.5,
      enemy.r / 4 + subY / 2,
      0,
      0,
      Math.PI * 2
    );
    ctx.fillStyle = "#000";
    ctx.fill();

    if (enemy.type === "boom") {
      //爆炸效果
      if (
        enemy.boomInterval < 50 &&
        calculateDistance({ x: myX, y: myY }, enemy) < Math.round(dpr * 180)
      ) {
        ctx.globalAlpha = 0.2;
        ctx.beginPath();
        ctx.arc(enemy.x, enemy.y, 100 * dpr, 0, 2 * Math.PI);
        ctx.fillStyle = "#ff0000";
        ctx.fill();
        if (enemy.boomInterval < 30) {
          ctx.globalAlpha = 0.5;
          ctx.beginPath();
          ctx.arc(
            enemy.x,
            enemy.y,
            3.3 * dpr * (30 - enemy.boomInterval),
            0,
            2 * Math.PI
          );
          ctx.fillStyle = "#ffffff";
          ctx.fill();
        }
      }
    }

    ctx.globalAlpha = 1;

    enemy.draw(ctx);

    // ctx.drawImage(enemyImage, enemy.x - r, enemy.y - r, 2 * r, 2 * r);
    //减少无敌时间
    if (enemy.invincible > 0) {
      enemy.invincible--;
    }
    //减少无敌时间
    if (enemy.invincibleForGuard > 0) {
      enemy.invincibleForGuard--;
    }
  }
}

//绘制我方
function drawMyself(ctx) {
  var boxLeft = $(".action-box").offset().left + 50;
  var dotLeft = $(".action").offset().left + 10;
  var boxTop = $(".action-box").offset().top + 50;
  var dotTop = $(".action").offset().top + 10;
  var subX =
    0.8 *
    dpr *
    (Math.abs(dotLeft - boxLeft) > 100
      ? dotLeft > boxLeft
        ? 4
        : -4
      : (dotLeft - boxLeft) / 25);
  var subY =
    0.8 *
    dpr *
    (Math.abs(dotTop - boxTop) > 100
      ? dotTop > boxTop
        ? 2
        : -2
      : (dotTop - boxTop) / 50);
  ctx.globalAlpha = 0.15;
  ctx.beginPath();
  ctx.ellipse(
    myX - subX,
    myY + r * 1.1 - subY,
    r / 1.2 + subY * 1.5,
    r / 4 + subY / 2,
    0,
    0,
    Math.PI * 2
  );
  ctx.fillStyle = "#000";
  ctx.fill();

  ctx.globalAlpha = 1;
  ctx.beginPath();
  ctx.arc(myX, myY, r, 0, 2 * Math.PI);
  ctx.fillStyle = "black";
  ctx.fill();

  if (dotLeft > boxLeft) {
    myDirection = "right";
  }
  if (dotLeft < boxLeft) {
    myDirection = "left";
  }
  if (dotLeft === boxLeft) {
    myDirection = "center";
  }

  var b = 1;

  var eyeSubX = subX * 1.7;

  if (myDirection === "right") {
    ctx.beginPath();
    ctx.ellipse(
      myX - r / 2.7 + eyeSubX,
      myY,
      r / 4.2 / b,
      r / 3 / b,
      0,
      0,
      Math.PI * 2
    );
    ctx.fillStyle = "#ffffff";
    ctx.fill();
    ctx.beginPath();
    ctx.ellipse(
      myX + r / 2.7 + eyeSubX,
      myY,
      r / 4.6 / b,
      r / 3.4 / b,
      0,
      0,
      Math.PI * 2
    );
    ctx.fillStyle = "#ffffff";
    ctx.fill();
  } else if (myDirection === "left") {
    ctx.beginPath();
    ctx.ellipse(
      myX + r / 2.7 + eyeSubX,
      myY,
      r / 4.2 / b,
      r / 3 / b,
      0,
      0,
      Math.PI * 2
    );
    ctx.fillStyle = "#ffffff";
    ctx.fill();
    ctx.beginPath();
    ctx.ellipse(
      myX - r / 2.7 + eyeSubX,
      myY,
      r / 4.6 / b,
      r / 3.4 / b,
      0,
      0,
      Math.PI * 2
    );
    ctx.fillStyle = "#ffffff";
    ctx.fill();
  } else {
    ctx.beginPath();
    ctx.ellipse(
      myX + r / 2.7 + eyeSubX,
      myY,
      r / 4.2 / b,
      r / 3 / b,
      0,
      0,
      Math.PI * 2
    );
    ctx.fillStyle = "#ffffff";
    ctx.fill();
    ctx.beginPath();
    ctx.ellipse(
      myX - r / 2.7 + eyeSubX,
      myY,
      r / 4.2 / b,
      r / 3 / b,
      0,
      0,
      Math.PI * 2
    );
    ctx.fillStyle = "#ffffff";
    ctx.fill();
  }
}

//绘制敌方子弹
function drawEnemyBullet(ctx) {
  for (
    var i = 0, enemyBulletListLength = enemyBulletList.length;
    i < enemyBulletListLength;
    i++
  ) {
    var enemyBullet = enemyBulletList[i];
    if (enemyBullet.type === "round") {
      enemyBullet.drawRound(ctx);
    } else if (enemyBullet.type === "triangle") {
      enemyBullet.drawTriangle(ctx, dpr);
    } else if (enemyBullet.type === "missile") {
      enemyBullet.drawMissile(ctx, dpr);
    }
  }
}

//绘制场地
function drawRectangle(ctx) {
  ctx.fillStyle = "#f7f7fb";
  // ctx.fillRect(battlegroundX, battlegroundY, battlegroundW, battlegroundH);
  ctx.beginPath();
  ctx.moveTo(battlegroundX + 20, battlegroundY); // 移动到左上角
  ctx.lineTo(battlegroundX + battlegroundW - 20, battlegroundY); // 绘制左上角到右上角的直线
  ctx.quadraticCurveTo(
    battlegroundX + battlegroundW,
    battlegroundY,
    battlegroundX + battlegroundW,
    battlegroundY + 20
  ); // 绘制右上角的圆角
  ctx.lineTo(battlegroundX + battlegroundW, battlegroundY + battlegroundH - 20); // 绘制右上角到右下角的直线
  ctx.quadraticCurveTo(
    battlegroundX + battlegroundW,
    battlegroundY + battlegroundH,
    battlegroundX + battlegroundW - 20,
    battlegroundY + battlegroundH
  ); // 绘制右下角的圆角
  ctx.lineTo(battlegroundX + 20, battlegroundY + battlegroundH); // 绘制右下角到左下角的直线
  ctx.quadraticCurveTo(
    battlegroundX,
    battlegroundY + battlegroundH,
    battlegroundX,
    battlegroundY + battlegroundH - 20
  ); // 绘制左下角的圆角
  ctx.lineTo(battlegroundX, battlegroundY + 20); // 绘制左下角到左上角的直线
  ctx.quadraticCurveTo(
    battlegroundX,
    battlegroundY,
    battlegroundX + 20,
    battlegroundY
  ); // 绘制左上角的圆角
  ctx.fill();
}

//绘制守卫
function drawGrid(ctx) {
  ctx.save();
  ctx.lineWidth = dpr * 0.1;
  ctx.strokeStyle = "rgba(0, 0, 0, 0.8)"; // 半透明的黑色，可根据需要调整

  const spacing = battlegroundH / 10;

  //水平偏移
  var subW = (battlegroundW % spacing) / 2;

  // 绘制垂直线
  for (
    let i = battlegroundX + spacing;
    i < battlegroundX + battlegroundW;
    i += spacing
  ) {
    ctx.beginPath();
    ctx.moveTo(i - spacing + subW, battlegroundY);
    ctx.lineTo(i - spacing + subW, battlegroundY + battlegroundH);
    ctx.stroke();
  }

  // 绘制水平线（保持相同的间距）
  for (
    let j = battlegroundY + spacing;
    j < battlegroundY + battlegroundH;
    j += spacing
  ) {
    ctx.beginPath();
    ctx.moveTo(battlegroundX, j);
    ctx.lineTo(battlegroundX + battlegroundW, j);
    ctx.stroke();
  }

  ctx.restore();
}
